<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>if判断语句</title>
</head>
<body>

<script>
    // 定义年龄变量,判断是否是未成年人
    let age = 9;
    if(age < 18){
        document.write("未成年");
        document.write("<br>");
    }else{
        document.write("已成年");
        document.write("<br>");
    }


    // JS非boolean作为if的条件
    // number: 非0为true, 0为false
    let num = 0;
    if (num){
        document.write("true");
        document.write("<br>");
    }else{
        document.write("false");
        document.write("<br>");
    }

    // string: 字符串有内容为true, 字符串没有内容("")为false
    let str = '1';
    if (str){
        document.write("true");
        document.write("<br>");
    }else{
        document.write("false");
        document.write("<br>");
    }
    // undefined: 为false
    let un;
    if (un){
        document.write("true");
        document.write("<br>");
    }else{
        document.write("false");
        document.write("<br>");
    }
    // object: 对象不为null是true, 对象为null是false
    let person = {name:"张三",age:18}
    document.write(person.name);
    if (person){
        document.write("true");
        document.write("<br>");
    }else{
        document.write("false");
        document.write("<br>");
    }
</script>

</body>
</html>